clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
* data from read_ibes.do
use permco permno datem permno LTG using  $Work/XSibes.dta, clear 

tsset permno datem
replace LTG=LTG/100
gen L1LTG=L12.LTG

gen dMFLTG=F1.LTG-LTG
gen dQFLTG=F3.LTG-LTG

forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
}
sort permco permno datem
merge 1:1 permco permno datem using $Work/EBR_new_monthly
drop if _merge==1
drop _merge SIZE
rename SI10 SIZE

gstats winsor FE* cp* STG2 *LTG, cuts(1 99) by(datem) replace
* -------------------------------------------- *
tempfile tmpstuff
save "`tmpstuff'", replace
* -------------------------------------------- *
foreach PORTF in BOOK {	
use if `PORTF'+qruf1<. using "`tmpstuff'", clear
generate portf=1+int((`PORTF'-1)/2)
order *, sequential    
keep portf datem ret_mo1-ret_mo60 LTG L1LTG d1LLTG FE* cp* d*FLTG STG2
gcollapse (mean) ret_mo1-ret_mo60 LTG L1LTG d1LLTG FE* cp* d*FLTG STG2, by(datem portf)
* -------------------------------------------- *
gen  cumNrawMo1=ret_mo1-1
* -------------------------------------------- *
gen  cumNrawQr1=ret_mo1*ret_mo2*ret_mo3-1
* -------------------------------------------- *
gen  cumNrawYr1=1 
forvalues x=1(1)12 {
	replace cumNrawYr1=cumNrawYr1*ret_mo`x'
}
replace cumNrawYr1=cumNrawYr1-1
* -------------------------------------------- *
gen  cumNrawYr3=1 
forvalues x=1(1)36 {
	replace cumNrawYr3=cumNrawYr3*ret_mo`x'
}
replace cumNrawYr3=cumNrawYr3-1
* -------------------------------------------- *
gen  cumNrawYr5=1 
forvalues x=1(1)60 {
	replace cumNrawYr5=cumNrawYr5*ret_mo`x'
}
replace cumNrawYr5=cumNrawYr5-1
* -------------------------------------------- *
tempfile tmp
save "`tmp'", replace
*********************
keep if portf==1 | portf==5 

foreach var of varlist cumNraw* LTG L1LTG d1LLTG FE* cp* d*FLTG STG2 {
replace `var'=-`var' if portf==5 & index("`PORTF'","INV")+index("`PORTF'","SIZE")==1
replace `var'=-`var' if portf==1 & index("`PORTF'","INV")+index("`PORTF'","SIZE")==0
}
foreach var of varlist cumNraw* LTG L1LTG d1LLTG FE* cp* d*FLTG STG2 {
	bys datem: egen n`var'=count(`var')
}
foreach var of varlist cumNraw* LTG L1LTG d1LLTG FE* cp* d*FLTG STG2 {
	replace `var'=. if n`var'<2
	drop n`var'
}
gcollapse (sum)        cumN* LTG L1LTG d1LLTG FE* cp* d*FLTG STG2,  by(datem)
foreach var of varlist cumN* LTG L1LTG d1LLTG FE* cp* d*FLTG STG2 {
	replace `var'=. if `var'==0
}
su cumN*
generate portf=6

append using `tmp'
sort portf datem
*********************
tsset portf datem
foreach var of varlist FE1-FE5 cp1-cp5 d*FLTG {
	quietly:  bys portf: egen sd=sd(`var')
	quietly: replace  `var'=`var'/sd	
	drop sd
}
eststo clear
foreach p of numlist 6 {
gen FE=FE1
gen cp=cp1
gen dFLTG=d1FLTG
eststo new_`p'_3: quietly: ivreg2 cumNrawYr1 FE cp dFLTG 	if portf==`p', bw(13)
drop FE cp dFLTG
gen FE=FE3
gen cp=cp3
gen dFLTG=d3FLTG
eststo new_`p'_4: quietly: ivreg2 cumNrawYr3 FE cp dFLTG 	if portf==`p', bw(37)
drop FE cp dFLTG
gen FE=FE5
gen cp=cp5
gen dFLTG=d5FLTG
eststo new_`p'_5: quietly: ivreg2 cumNrawYr5 FE cp dFLTG 	if portf==`p', bw(61)
drop FE cp dFLTG
}
esttab, order(FE cp dFLTG)
local file "AppendixB13_June"
if "`PORTF'"=="BOOK" {
esttab new* using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) replace  title(`PORTF')
}
if "`PORTF'"!="BOOK" {
esttab new* using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) append   title(`PORTF')
}
}
